<template>
  <el-container>
    <!-- 头部 -->
    <el-header>
      <div>
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>

    <!-- 主体 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="iscollapse ? '64px' : '200px'">
        <div class="toggleBtn" @click="toggleCollapse">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#5ba3ff"
          unique-opened
          :collapse="iscollapse"
          :collapse-transition="false"
          router
          :default-active="active"
        >
          <!-- unique-opened:是否只保持一个子菜单的展开
          collapse:是否水平折叠收起菜单
          collapse-transition:是否开启折叠动画
          router:是否使用vue-router的模式，启用该模式会在激活导航时以 index 作为 path 进行路由跳转
          -->
          <!-- 一级菜单 -->
          <el-submenu :index="value.id + ''" :key="value.id" v-for="value in menuList">
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[value.id]"></i>
              <!-- 文本 -->
              <span>{{ value.authName }}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + item.path"
              :key="item.id"
              v-for="item in value.children"
              @click="saveNavState('/' + item.path)"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ item.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <!-- 右侧主体区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      iconsObj: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao',
      },
      iscollapse: false,
      //被激活的链接
      active: '',
    }
  },
  created() {
    this.getMenu()
    this.active = window.sessionStorage.getItem('active')
  },
  methods: {
    logout() {
      // 清空token
      window.sessionStorage.clear()
      // 跳转到登录页
      this.$router.push('/login')
    },
    async getMenu() {
      const { data: data } = await this.$axios.get('menus')
      // console.log(data)
      if (data.meta.status !== 200) return this.$message.error('data.meta.msg')
      this.menuList = data.data
    },
    //点击按钮 进行折叠侧边栏
    async toggleCollapse() {
      this.iscollapse = !this.iscollapse
    },
    //保存链接的激活状态
    saveNavState(activePath) {
      window.sessionStorage.setItem('active', activePath)
    },
  },
}
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
}
.el-header {
  background-color: #373e41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  color: #fff;
  font-size: 20px;

  div {
    display: flex;
    align-items: center;

    span {
      margin-left: 20px;
      display: flex;
      align-content: center;
    }
  }
}
.el-aside {
  background-color: #333744;
  text-align: center;
  .toggleBtn {
    background-color: #4a5064;
    font-size: 20px;
    line-height: 30px;
    color: #ccc;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #e4e8ec;
}
.iconfont {
  margin-right: 10px;
  font-size: 20px;
}
</style>
